struct Point {
    x: f64,
    y: f64,
}

impl Point {
    fn origin() -> Point {
        return Point { x: 0.0, y: 0.0 };
    }
    fn new(x: f64, y: f64) -> Point {
        return Point { x: x, y: y };
    }
}

struct Rectangle {
    LU: Point,
    RD: Point,
}

impl Rectangle {
    // 面积
    fn area(&self) -> f64 {
        let Point { x: x1, y: y1 } = self.LU;
        let Point { x: x2, y: y2 } = self.RD;
        return ((x1 - x2) * (y1 - y2)).abs();
    }
    // 周长
    fn perimeter(&self) -> f64 {
        let Point { x: x1, y: y1 } = self.LU;
        let Point { x: x2, y: y2 } = self.RD;
        return 2.0 * ((x1 - x2).abs() + (y1 - y2).abs());
    }
    fn translate(&mut self, x: f64, y: f64) {
        self.LU.x += x;
        self.LU.y += y;
        self.RD.x += x;
        self.RD.y += y;
    }
    fn new(lu: Point, rd: Point) -> Rectangle {
        Rectangle { LU: lu, RD: rd }
    }
}

struct Pair(Box<i32>, Box<i32>);

impl Pair {
    fn destroy(self) {
        let Pair(first, second) = self;
        println!("Destroying Pair ({},{})", first, second);
    }
}

// // `Pair` 拥有资源：两个堆分配的整型
// struct Pair(Box<i32>, Box<i32>);

// impl Pair {
//     fn destroy(self) {
//         let Pair(first, second) = self;
//         println!("Destroying Pair({}, {})", first, second);
//     }
// }

fn main() {
    println!("Hello, world!");
    let rectangle = Rectangle::new(
        Point::origin(), Point::new(3.0, 4.0),
    );
    println!("Rectangle perimeter: {}", rectangle.perimeter());
    println!("Rectangle area: {}", rectangle.area());

    // rectangle.translate(1.0, 1.0);

    let mut square = Rectangle::new(
        Point::origin(), Point::new(1.0, 1.0),
    );

    square.translate(1.0, 1.0);

    let pair = Pair(Box::new(1), Box::new(2));
    pair.destroy();

}
























